/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.front.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.front.domain.MaterialStockFront;
import me.zhengjie.front.domain.vo.MaterialStockQueryCriteria;
import me.zhengjie.front.mapper.MaterialStockFrontMapper;
import me.zhengjie.front.service.MaterialStockService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @description 服务实现
* @author XX
* @date 2024-05-23
**/
@Service
@RequiredArgsConstructor
public class MaterialStockServiceImpl extends ServiceImpl<MaterialStockFrontMapper, MaterialStockFront> implements MaterialStockService {

    private final MaterialStockFrontMapper materialStockFrontMapper;

    @Override
    public PageResult<MaterialStockFront> queryAll(MaterialStockQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(materialStockFrontMapper.findAll(criteria, page));
    }

    @Override
    public List<MaterialStockFront> queryAll(MaterialStockQueryCriteria criteria){
        return materialStockFrontMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MaterialStockFront resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MaterialStockFront resources) {
        MaterialStockFront materialStockFront = getById(resources.getId());
        materialStockFront.copy(resources);
        saveOrUpdate(materialStockFront);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<MaterialStockFront> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MaterialStockFront materialStockFront : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("物料序列号", materialStockFront.getSmallBoxCode());
            map.put("物料所属箱号", materialStockFront.getMediumBoxCode());
            map.put("物料编码", materialStockFront.getMaterialCode());
            map.put("绑定的栈板码", materialStockFront.getPalletCode());
            map.put("入库单号", materialStockFront.getInboundOrderCode());
            map.put("批次号", materialStockFront.getBatchCode());
            map.put("入库日期", materialStockFront.getInboundDate());
            map.put("生产线", materialStockFront.getProductionLine());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}